package com.shinobi.timetracker;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import com.shinobi.timetracker.database.GlobalState;

/**
 * Created by IntelliJ IDEA. User: Semy Date: 10.05.11 Time: 12:07 To change
 * this template use File | Settings | File Templates.
 */
public class TagsActivity extends Activity {

	private GlobalState globalState;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.tagseditview);

		this.globalState = (GlobalState) getApplication();

		InitializeSpinner();
		InitializeAddItemButton();
		// InitializeRemoveItemButton();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		InitializeSpinner();
	}
	
	private void InitializeSpinner() {

		Spinner tagComboBox = (Spinner) findViewById(R.id.tagComboBox);
		
		ArrayList<String> tags = globalState.GetTags(this);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, tags);	
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		tagComboBox.setAdapter(adapter);

        String defaultTag  = globalState.CurrentTag();
        int position = adapter.getPosition(defaultTag);
        
        if (position < 0) {
        	position = 0;
        	tagComboBox.setSelection(position);
        	globalState.SetCurrentTag((String)tagComboBox.getSelectedItem());
        } else {
        	tagComboBox.setSelection(position);
        }

		tagComboBox.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parentView,
					View selectedItemView, int position, long id) {

				GlobalState globalState = (GlobalState) getApplication();
				Spinner tagComboBox = (Spinner) findViewById(R.id.tagComboBox);
				String tag = (String) tagComboBox.getSelectedItem();
				globalState.SetCurrentTag(tag);
				InitializeSpinner();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				globalState.SetCurrentTag("");
			}

		});
	}

	private void InitializeAddItemButton() {

		Button addItemBtn = (Button) findViewById(R.id.addSpinnerItemButton);
		final TagsActivity t = this;
		addItemBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				GlobalState globalState = (GlobalState) getApplication();
				TextView textView = (TextView) findViewById(R.id.newTagTextBox);
				String tag = textView.getText().toString();
				globalState.AddTag(t, tag);
			}
		});
	}

	private void InitializeRemoveItemButton() {

		// Button remItemBtn = (Button) findViewById(R.id.remSpinnerItemButton);
		// remItemBtn.setOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View view) {
		// GlobalState globalState = (GlobalState) getApplication();
		// Spinner spinner = (Spinner) findViewById(R.id.tagComboBox);
		// String tag = spinner.getSelectedItem().toString();
		// //globalState.RemoveTag(this, tag);
		// }
		// });
	}

}